{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d96a87ee-465b-452e-89e4-8edf7742a9cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup OpenAI Agent\n",
    "import openai\n",
    "\n",
    "openai.api_key = \"sk-your-key\"\n",
    "from llama_index.agent import OpenAIAgent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0a8b5383-e780-45fa-9c18-9fd47954f43d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import and initialize our tool spec\n",
    "from llama_index.tools.tool_spec.load_and_search.base import LoadAndSearchToolSpec\n",
    "from llama_hub.tools.google_search.base import GoogleSearchToolSpec\n",
    "\n",
    "google_spec = GoogleSearchToolSpec(key=\"your-key\", engine=\"your-engine\")\n",
    "\n",
    "# Wrap the google search tool as it returns large payloads\n",
    "tools = LoadAndSearchToolSpec.from_defaults(\n",
    "    google_spec.to_tool_list()[0],\n",
    ").to_tool_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c430b2cd-32be-4ca6-b907-f420c1f6c78c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create the Agent with our tools\n",
    "agent = OpenAIAgent.from_tools(tools, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "97378143-3ec9-4942-9e1b-15fbb8b4561d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: google_search with args: {\n",
      "  \"query\": \"Barack Obama\"\n",
      "}\n",
      "Got output: Content loaded! You can now search the information using read_google_search\n",
      "========================\n",
      "=== Calling Function ===\n",
      "Calling function: read_google_search with args: {\n",
      "  \"query\": \"Who is Barack Obama?\"\n",
      "}\n",
      "Got output: \n",
      "Barack Obama is the 44th President of the United States. He is a man with values from the heartland, a middle-class upbringing in a strong family, and a conviction that a life so blessed should be lived in service to others.\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='Barack Obama is the 44th President of the United States. He is known for his middle-class upbringing, strong family values, and his belief in serving others.', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"who is barack obama\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ee34c3fc-2d06-4d77-b489-9870d880b6ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: google_search with args: {\n",
      "  \"query\": \"Barack Obama last visit to Michigan\"\n",
      "}\n",
      "Got output: Content loaded! You can now search the information using read_google_search\n",
      "========================\n",
      "=== Calling Function ===\n",
      "Calling function: read_google_search with args: {\n",
      "  \"query\": \"When was the last time Barack Obama visited Michigan?\"\n",
      "}\n",
      "Got output: \n",
      "The last time Barack Obama visited Michigan was on October 29, 2022, 11 days before the November 8 election.\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='The last time Barack Obama visited Michigan was on October 29, 2022, which was 11 days before the November 8 election.', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"when is the last time barrack obama visited michigan\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "34b4a6c7-ab04-4505-bd6c-d1966cf85ee2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: google_search with args: {\n",
      "  \"query\": \"Barack Obama visits to Michigan\"\n",
      "}\n",
      "Got output: Content loaded! You can now search the information using read_google_search\n",
      "========================\n",
      "=== Calling Function ===\n",
      "Calling function: read_google_search with args: {\n",
      "  \"query\": \"Barack Obama visits to Michigan\"\n",
      "}\n",
      "Got output: \n",
      "Barack Obama is visiting Michigan this month to campaign for Michigan Democrats as part of his first nationwide tour for prominent Democrats in key races.\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='Barack Obama has recently visited Michigan as part of his nationwide tour to campaign for Michigan Democrats in key races.', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"when else did he visit michigan\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bfa4c600-b3dc-4847-b36e-5da1ba00763b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: google_search with args: {\n",
      "  \"query\": \"Barack Obama favorite sport\"\n",
      "}\n",
      "Got output: Content loaded! You can now search the information using read_google_search\n",
      "========================\n",
      "=== Calling Function ===\n",
      "Calling function: read_google_search with args: {\n",
      "  \"query\": \"What is Barack Obama's favorite sport?\"\n",
      "}\n",
      "Got output: \n",
      "Barack Obama's favorite sport is basketball.\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response=\"Barack Obama's favorite sport is basketball.\", source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"what is his favourite sport\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
